package com.umeox.watch.moto.alarm.adapter;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.umeox.moto.common.rvadapter.MultiItemTypeAdapter;
import com.umeox.moto.common.rvadapter.ViewHolder;
import com.umeox.moto.common.rvadapter.wrapper.ItemViewDelegate;
import com.umeox.moto.watch.themes.widget.ImageButton;
import com.umeox.watch.moto.alarm.AlarmDetail;
import com.umeox.watch.moto.alarm.R;
import com.umeox.watch.moto.alarm.Utils;

import java.util.List;
import java.util.Locale;

public class AlarmAdapter extends MultiItemTypeAdapter<AlarmDetail> {
    private OnItemClickListener mOnItemClickListener;
    private final boolean m24HourFormat;

    public void setOnClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(AlarmDetail alarmDetail);
    }

    public AlarmAdapter(Context context, List<AlarmDetail> alarmList) {
        super(context, alarmList);
        m24HourFormat = DateFormat.is24HourFormat(context);
        addItemViewDelegate(new AlarmDetailDelegate());
    }

    public AlarmDetail getItem(int position) {
        if (getDatas() != null && position < getDatas().size()) {
            return getDatas().get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private class AlarmDetailDelegate implements ItemViewDelegate<AlarmDetail> {
        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_alarm_clock;
        }

        @Override
        public boolean isForViewType(AlarmDetail contactInfo, int position) {
            return true;
        }

        @Override
        public void convert(ViewHolder viewHolder, final AlarmDetail detail, int position) {
            TextView tvTime = viewHolder.getView(R.id.alarm_time);
            TextView tvAmpm = viewHolder.getView(R.id.alarm_ampm);
            final View itemView = viewHolder.getView(R.id.item);
            TextView tvRepeat = viewHolder.getView(R.id.alarm_repeat_day);
            Switch state = viewHolder.getView(R.id.stateSwitch);
            View remoteIv = viewHolder.getView(R.id.ivRemote);
            tvAmpm.setVisibility(m24HourFormat ? View.GONE : View.VISIBLE);
            final String alarmTime = detail.getAlarmTime();
            tvTime.setText(m24HourFormat ? Utils.to24HourFormat(alarmTime, Locale.getDefault()) : Utils.to12HourFormat(alarmTime, Locale.getDefault()));
            tvAmpm.setText(detail.getHour() < 12 ? mContext.getString(R.string.am) : mContext.getString(R.string.pm));

            final boolean language_ru = "ru".equals(Locale.getDefault().getLanguage());
            final String repeatExpression = detail.getRepeatExpression();
            final String repeatDay = Utils.getRepeatDay(mContext, repeatExpression, null, ' ');
            tvRepeat.setText(language_ru ? Utils.monTOSunOrder(repeatExpression, repeatDay) : repeatDay);

            final boolean remoteAlarm = detail.getAlarmId() > 0;
            if (remoteAlarm) {
                remoteIv.setVisibility(View.VISIBLE);
                state.setVisibility(View.GONE);
            } else {
                viewHolder.getView(R.id.ivRemote).setVisibility(View.GONE);
                state.setVisibility(View.VISIBLE);
            }
            boolean enable = detail.isValid();
            state.setChecked(enable);
            tvTime.setEnabled(enable);
            tvAmpm.setEnabled(enable);
            tvRepeat.setEnabled(enable);

            state.setOnClickListener(v -> {
                detail.setState(state.isChecked() ? 1 : 0);
                v.postDelayed(detail::update, 200);
            });


            itemView.setOnClickListener(v -> {
                if (mOnItemClickListener != null && !remoteAlarm) {
                    mOnItemClickListener.onItemClick(detail);
                }
            });
        }

    }

    @Override
    public ViewHolder footViewHOlder(ViewGroup parent) {
        ImageButton view = new ImageButton(parent.getContext());
        view.setImageResource(R.drawable.alarm_clock_add_selector);
        view.setPadding(0,23,0,10);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 73);
        view.setLayoutParams(layoutParams);
        view.setBackground(null);
        view.setOnClickListener(v -> {
            if (mOnItemClickListener != null ) {
                mOnItemClickListener.onItemClick(null);
            }
        });
        return new FootViewHolder(view);
    }

    class FootViewHolder extends ViewHolder {
        public FootViewHolder(@NonNull View itemView) {
            super(itemView.getContext(), itemView);
        }
    }
}
